package com.corechina.apex_osswords.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.domain.entity.CoreFiles;
import com.corechina.apex_osswords.domain.vo.ImageStaticVo;
import com.corechina.apex_osswords.service.ICoreFilesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 文件表 前端控制器
 * </p>
 *
 * @author cc
 * @since 2022-03-11
 */
@Api(tags = "文件管理")
@RestController
@RequestMapping("/core/files")
public class CoreFilesController {

    @Autowired
    private ICoreFilesService iCoreFilesService;

    @ApiOperation("图片上传")
    @PostMapping("/uploadImage")
    public Response uploadImage(@RequestParam(value = "file", required = false) MultipartFile file, String guidKey) {
        return iCoreFilesService.uploadImage(file, guidKey);
    }

    @ApiOperation("文件上传")
    @PostMapping("/uploadFile")
    public Response uploadFile(@RequestParam(value = "file", required = false) MultipartFile file, String guidKey) {
        return iCoreFilesService.uploadFile(file, guidKey);
    }

    @ApiOperation("/下载文件")
    @GetMapping("/downloadFile")
    public void downloadFile(HttpServletResponse resp, Integer id) {
        iCoreFilesService.downloadFile(resp, id);
    }

    @ApiOperation("/删除文件")
    @DeleteMapping("/delFile")
    public Response delFile(Integer id) {
        return iCoreFilesService.delFile(id);
    }

    @ApiOperation("/获取文件列表")
    @GetMapping("/getListByGuidKey")
    public Response getListByGuidKey(String guidKey) {
        QueryWrapper<CoreFiles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("guid_key", guidKey).eq("is_del", 0).orderByAsc("id");
        return Response.ok(iCoreFilesService.list(queryWrapper));
    }

    @ApiOperation("/获取图片列表-base64")
    @GetMapping("/getImageListByGuidKey")
    public Response getImageListByGuidKey(String guidKey, Integer type) {
        return iCoreFilesService.getImageListByGuidKey(guidKey, type);
    }

    @ApiOperation("/获取单个图片-base64")
    @GetMapping("/getById")
    public Response getById(Integer id, Integer type) {
        return Response.ok(iCoreFilesService.getById(id, type));
    }

    @ApiOperation("静态图片上传")
    @PostMapping("/uploadImageForStatic")
    public ImageStaticVo uploadImageForStatic(@RequestParam(value = "file", required = false) MultipartFile file) {
        return iCoreFilesService.uploadImageForStatic(file);
    }
}

